#include "../include/common.h"

/* PS: 注意长度受限和不受限的区别
    不受限制：寻找字符串尾部的'\0'，由调用者保证不会溢出内存
    受限：只考虑 len 个字符，不保证 '\0'

*/ 


int my_strlen(char const *s){
    // char *p = s; // without const
    // while (*p++);
    // return p - s;
    int count = 0;
    while (*s++)    // while (*s++ != '\0')
        count++;
    return count;
}

char *my_strcpy(char *dest, char const *src){
    while(*dest++ = *src++);
    *dest = '\0'; // 很重要
    return dest;
}

char *my_strcat(char *dest, char const *src){
    while(*dest++);
    dest--;
    while(*dest++ = *src++);
    *dest = '\0';
    return dest;
}

int my_strcmp2(char const *s1, char const *s2){
    while (*s1++ == *s2++) {
        if (*s1 == '\0' || *s2 == '\0')
            break;
            // return 0;
    }
    printf("s1=%s, s2=%s.\n", s1, s2);
    if (*s1 == '\0')
        if (*s2 == '\0')
            return 0;
        else 
            return -1;
    else {
        if (*s2 == '\0')
            return 1;
        else
            return -2;
    }
}

int my_strcmp(char const *s1, char const *s2){
    while (*s1++ == *s2++ && *s1 != '\0');
    return *s1 - *s2;
}


int my_atoi(char const *str){
    if(str == NULL)
    {
        printf("Invalid Input"); 
        return -1;
    }
    
    while(*str ==' ')   //去掉字符串开始的空格
        str++;

    int nSign = (*str == '-') ? -1 : 1; //确定符号位
    if(*str == '+'|| *str == '-')
        str++;
    
    int nResult = 0;
    while(*str >= '0' && *str <= '9')
    {
        nResult = nResult * 10 + (*str - '0');
        str++;
    }

    while(*str == ' ')
        str++;
    
    if(*str != '\0'){
        printf("Invalid Input");
        return -1;
    }

    return nResult*nSign;
}

char *my_itoa(char *str, int num, int radix){
    char *ptr = str;
    int temp;
    int sign = num, i = 0, j = 0;

    if(sign < 0)
        num =-num;

    while (num){
        //先存储低位
        *ptr++ = num % radix + '0';
        num /= radix;
        //小于进制数直接放进去
        if(num<radix)
        {
            *ptr++=num+'0';
            break;
        }
    }

    //最高位存储符号位
    if(sign<0)
    {
        *ptr++='-';
    }
    *ptr = '\0';

    //存储的是反过来的 我们需要调换位置
    j = ptr-str-1;
    for(i=0; i<j; i++){
        int temp = str[i];
        str[i] = str[j];
        str[j--]=temp;
    } 
    return str;
}

void *my_memmove(void * const dest, const void * const src, unsigned int n){
    // check parameters
    if (0 == n) {
        return NULL;
    }
    if (NULL == dest || NULL == src) {
        return NULL;
    }
    char * psrc = (char *)src;
    char * pdest = (char *)dest;
    if (pdest <= psrc || pdest > psrc + n) {
         printf("forward overlapping\n");
         // copy forward direction
         for(size_t i = 0; i < n; i++) {
             *pdest = *psrc;
             pdest++;
             psrc++;
         }
    }
    else {
        printf("backward overlapping\n");
        // copy backward direction
        pdest = pdest + n;
        psrc = psrc + n;
        for (size_t i = 0; i< n; i++) {
            *pdest = *psrc;
            pdest--;
            psrc--;
        }
    }
    return dest;
}